package cn.ticket.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ticket.model.NewsSet;
import cn.ticket.service.NewsSetService;
import cn.ticket.service.Utility;


@Controller
@RequestMapping("newsSet")
public class NewsSetController {

	@Autowired
	NewsSetService newsSetService;
	
	@RequestMapping("")
	public String noticeIndex(){
		return "news_set";
	}
	@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String insert(@RequestBody NewsSet newsSet){
		Map<String, Object> result=new HashMap<>();
		int num=newsSetService.insertSelective(newsSet);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="update",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String update(@RequestBody NewsSet newsSet){
		Map<String, Object> result=new HashMap<>();
		int num=newsSetService.updateByPrimaryKeySelective(newsSet);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="delete",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String delete(@RequestBody Map<String, String> args){
		Map<String, Object> result=new HashMap<>();
		int id=Integer.parseInt(args.get("id"));
		int num=newsSetService.deleteByPrimaryKey(id);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getAll",produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getAll(){
		Map<String, Object> result=new HashMap<>();
		List<NewsSet> NewsSet=newsSetService.getAll();
		if (!NewsSet.isEmpty()) {
			result.put("status", 1001);
			result.put("body", NewsSet);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
}
